package com.bosssoft.boss_exam_db.validator.common;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

/***
 * @Author lys(cherish)
 * @Phone 17720799636
 * @Date  2019/3/13
 * @Descriptionh 用户名校验
 **/
public class MailValidator implements ConstraintValidator<Mail, String> {

    private static Pattern PHONE_PATTERN =null;

    @Override
    public void initialize(Mail mail) {
        PHONE_PATTERN= compile("\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}");
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if ( value == null || value.length() == 0 ) {
            return true;
        }
        Matcher m = PHONE_PATTERN.matcher(value);
        return m.matches();
    }
}
